package lianxi;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;

public class demo6 {
    public class Solution {
        public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
       ArrayList<Integer> list=new ArrayList<>();
            PriorityQueue<Integer> queue=new PriorityQueue<>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o2.compareTo(o1);
                }
            });
            //判断如果长度小于k，加数据
            //数据比根元素小，删除跟，替换
            for (int i = 0; i < input.length; i++) {
                if(queue.size()!=k){
                    queue.add(input[i]);
                }
                else if(queue.peek()>input[i]){
                    queue.poll();
                    queue.add(input[i]);
                }
            }
                if(input.length==0||k<input.length||k<=0){
                    return list;
                }
                Iterator<Integer> it = queue.iterator();
                while (it.hasNext()) {
                    list.add(it.next());
                }










       return list;
        }
    }
}
